package com.gdth.bank.model;

import java.util.List;

import rx.Observable;
import rx.Subscriber;

import com.gdth.bank.BankApplication;
import com.gdth.bank.utils.AppUtil;
import com.gdth.database.bean.DaoSession;
import com.gdth.database.bean.TAppBfrjBfjlDao;
import com.gdth.database.bean.TAppBfrjJbxxb;
import com.gdth.database.bean.TAppBfrjJbxxbDao;
import com.gdth.database.bean.TAppBfrjJtcyb;
import com.gdth.database.bean.TAppBfrjJtcybDao;
import com.gdth.database.bean.TAppBfrjLxfs;
import com.gdth.database.bean.TAppBfrjLxfsDao;
import com.gdth.database.bean.TAppBfrjZcxxb;
import com.gdth.database.bean.TAppBfrjZcxxbDao;

import org.greenrobot.greendao.query.QueryBuilder;


public class TAppBfrjJtcybModel {
	public Observable<TAppBfrjJtcyb> savejtcyxx(final TAppBfrjJtcyb mTAppBfrjJtcyb ,final String jcuuid,int type , boolean isHuzhu){
		return Observable.create(new Observable.OnSubscribe<TAppBfrjJtcyb>() {

			@Override
			public void call(Subscriber<? super TAppBfrjJtcyb> arg0) {
				// TODO Auto-generated method stub
				boolean re = true;
				Exception ex = null;
				TAppBfrjJtcybDao dao = BankApplication.getDaoSession().getTAppBfrjJtcybDao();
				if(type ==0){   //  时间: 2020/5/27 9:19 AM ,作者: xjc, 作用: 新增保存
					String zjhm=mTAppBfrjJtcyb.getZjhm();
					QueryBuilder<TAppBfrjJtcyb> qb = dao.queryBuilder();
					qb.where(TAppBfrjJtcybDao.Properties.Jcuuid.eq(jcuuid));
					qb.where(TAppBfrjJtcybDao.Properties.Zjhm.eq(zjhm),
							qb.or(TAppBfrjJtcybDao.Properties.Sjzt.eq(AppUtil.sjzt_create), TAppBfrjJtcybDao.Properties.Sjzt.eq(AppUtil.sjzt_update), TAppBfrjJtcybDao.Properties.Sjzt.eq(AppUtil.sjzt_normal)));
					List<TAppBfrjJtcyb> mList = qb.list();
					if(mList.size()<1){
						dao.insert(mTAppBfrjJtcyb);
						CJkhxxModel.updateCjkhJbxxZt(mTAppBfrjJtcyb.getJcuuid());
						arg0.onNext(mTAppBfrjJtcyb);
						arg0.onCompleted();
					}else{
						arg0.onNext(null);
						arg0.onError(null);
					}
				}else{ //  时间: 2020/5/9 1:42 PM ,作者: xjc, 作用: 修改保存
					try {
						if (isHuzhu){ //  时间: 2020/5/27 9:20 AM ,作者: xjc, 作用: 是户主
							TAppBfrjJbxxbDao bxxbdao = BankApplication.getDaoSession().getTAppBfrjJbxxbDao();
							QueryBuilder<TAppBfrjJbxxb> qb = bxxbdao.queryBuilder();
							qb.where(TAppBfrjJbxxbDao.Properties.Uuid.eq(mTAppBfrjJtcyb.getJcuuid()));
							List<TAppBfrjJbxxb> mList = qb.list();
							if(mList.size() > 0){
								TAppBfrjJbxxb bfrjJbxxb = mList.get(0);
								bfrjJbxxb.setZjhm(mTAppBfrjJtcyb.getZjhm());
								bfrjJbxxb.setKhxm(mTAppBfrjJtcyb.getCyxm());
								bfrjJbxxb.setXgsj(mTAppBfrjJtcyb.getXgsj());
								bxxbdao.update(bfrjJbxxb);
							}
						}
						dao.insertOrReplace(mTAppBfrjJtcyb);
						CJkhxxModel.updateCjkhJbxxZt(mTAppBfrjJtcyb.getJcuuid());
					} catch(Exception e) {
						ex = e;
						re = false;
					}
					if (re){
						arg0.onNext(mTAppBfrjJtcyb);
						arg0.onCompleted();
					} else{
						arg0.onError(ex);
					}
				}

			/*if(mTAppBfrjJtcyb==null){
				arg0.onError(null);
			}else{
				arg0.onNext(mTAppBfrjJtcyb);
				arg0.onCompleted();
			}*/
			
			}
		});
	}
	
	public Observable<List<TAppBfrjJtcyb>> getjtcyxxUuid(final String jcuuid){
		return Observable.create(new Observable.OnSubscribe<List<TAppBfrjJtcyb>>() {

			@Override
			public void call(Subscriber<? super List<TAppBfrjJtcyb>> arg0) {
				// TODO Auto-generated method stub
				TAppBfrjJtcybDao dao = BankApplication.getDaoSession().getTAppBfrjJtcybDao();
				QueryBuilder<TAppBfrjJtcyb> qb = dao.queryBuilder();

				qb.where(TAppBfrjJtcybDao.Properties.Jcuuid.eq(jcuuid),
						qb.or(TAppBfrjJtcybDao.Properties.Sjzt.eq(AppUtil.sjzt_create), TAppBfrjJtcybDao.Properties.Sjzt.eq(AppUtil.sjzt_update), TAppBfrjJtcybDao.Properties.Sjzt.eq(AppUtil.sjzt_normal)));

				List<TAppBfrjJtcyb> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}
	public Observable<TAppBfrjJtcyb> getjtcyUuid(final String jtuuid){
		return Observable.create(new Observable.OnSubscribe<TAppBfrjJtcyb>() {

			@Override
			public void call(Subscriber<? super TAppBfrjJtcyb> arg0) {
				// TODO Auto-generated method stub
				TAppBfrjJtcybDao dao = BankApplication.getDaoSession().getTAppBfrjJtcybDao();
				QueryBuilder<TAppBfrjJtcyb> qb = dao.queryBuilder();

				qb.where(TAppBfrjJtcybDao.Properties.Uuid.eq(jtuuid));

				List<TAppBfrjJtcyb> mList = qb.list();
				TAppBfrjJtcyb mTAppBfrjJtcyb= mList.get(0);
				if (mTAppBfrjJtcyb == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mTAppBfrjJtcyb);
					arg0.onCompleted();
				}
			}
		});
	}

	public Observable<String> getbxxbZJHMM(final String jtuuid){
		return Observable.create(new Observable.OnSubscribe<String>() {

			@Override
			public void call(Subscriber<? super String> arg0) {
				// TODO Auto-generated method stub
				TAppBfrjJbxxbDao bxxbdao = BankApplication.getDaoSession().getTAppBfrjJbxxbDao();
				QueryBuilder<TAppBfrjJbxxb> qb = bxxbdao.queryBuilder();
				qb.where(TAppBfrjJbxxbDao.Properties.Uuid.eq(jtuuid),TAppBfrjJbxxbDao.Properties.Sjzt.notEq(AppUtil.sjzt_delete));
				List<TAppBfrjJbxxb> mList = qb.list();
				if(mList.size() > 0){ //  时间: 2020/5/26 8:58 PM ,作者: xjc, 作用: 户主不能被修改
					arg0.onNext(mList.get(0).getZjhm());
					arg0.onCompleted();
				}else{
					arg0.onNext("");
					arg0.onCompleted();
				}
			}
		});
	}

	public Observable<String> deletejtcy(TAppBfrjJtcyb mTAppBfrjJtcyb){
		final TAppBfrjJtcyb tAppBfrjJtcyb = mTAppBfrjJtcyb;
		return Observable.create(new Observable.OnSubscribe<String>() {

			@Override
			public void call(final Subscriber<? super String> arg0) {
				// TODO 采访客户的资产信息
				final DaoSession session = BankApplication.getDaoSession();
				session.runInTx(new Runnable() {

					@Override
					public void run() {
						boolean re = true;
						Exception ex = null;
						TAppBfrjZcxxbDao zcxxdao = session.getTAppBfrjZcxxbDao();
						QueryBuilder<TAppBfrjZcxxb> zcxxqb = zcxxdao.queryBuilder();
						zcxxqb.where(TAppBfrjZcxxbDao.Properties.Zjhm.eq(tAppBfrjJtcyb.getZjhm()),
								TAppBfrjZcxxbDao.Properties.Jcuuid.eq(tAppBfrjJtcyb.getJcuuid())
								,TAppBfrjZcxxbDao.Properties.Sjzt.notEq(AppUtil.sjzt_delete));
						List<TAppBfrjZcxxb> zcxxList = zcxxqb.list();
						if(zcxxList.size() > 0){	//  时间: 2020/5/27 9:10 AM ,作者: xjc, 作用:关联资产不能被删除
							if (re){
								arg0.onNext(String.valueOf(2));
								arg0.onCompleted();
							} else{
								arg0.onError(ex);
							}
						}else{
							//  时间: 2020/5/27 10:16 AM ,作者: xjc, 作用: 联系方式关联不能删除
							TAppBfrjLxfsDao lxfsdao = session.getTAppBfrjLxfsDao();
							QueryBuilder<TAppBfrjLxfs> lxfsqb = lxfsdao.queryBuilder();
							lxfsqb.where(TAppBfrjLxfsDao.Properties.Jtuuid.eq(tAppBfrjJtcyb.getUuid())
									,TAppBfrjLxfsDao.Properties.Sjzt.notEq(AppUtil.sjzt_delete));
							List<TAppBfrjLxfs> lxfsList = lxfsqb.list();
							if(lxfsList.size() > 0){
								if (re){
									arg0.onNext(String.valueOf(3));
									arg0.onCompleted();
								} else{
									arg0.onError(ex);
								}
							}else{
								TAppBfrjJtcybDao dao = session.getTAppBfrjJtcybDao();
								try {
									tAppBfrjJtcyb.setSjzt(AppUtil.sjzt_delete);
									dao.insertOrReplace(tAppBfrjJtcyb);
									CJkhxxModel.updateCjkhJbxxZt(tAppBfrjJtcyb.getJcuuid());
								} catch(Exception e) {
									ex = e;
									re = false;
								}
								if (re){
									arg0.onNext(String.valueOf(1));
									arg0.onCompleted();
								} else{
									arg0.onError(ex);
								}
							}

						}

					}
				});
			}
		});
	}
}
